package com.tiancheng.trade.authserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.trade.authserver.constant.PermissionCodeConstant;
import com.tiancheng.trade.authserver.entity.AuthApplication;
import com.tiancheng.trade.authserver.entity.AuthPermission;
import com.tiancheng.trade.authserver.entity.AuthRole;
import com.tiancheng.trade.authserver.mapper.AuthApplicationMapper;
import com.tiancheng.trade.authserver.mapper.AuthPermissionMapper;
import com.tiancheng.trade.authserver.mapper.AuthRoleMapper;
import com.tiancheng.trade.authserver.service.IAuthApplicationService;
import com.tiancheng.trade.authserver.service.IAuthPermissionService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: likailun
 * @Description: 授权角色servie实现
 * @Date: create in 2024/10/21 17:21
 */
@Service
public class AuthPermissionServiceImpl extends ServiceImpl<AuthPermissionMapper, AuthPermission> implements IAuthPermissionService {

    @Resource
    private AuthApplicationMapper authApplicationMapper;
    @Resource
    private AuthRoleMapper authRoleMapper;
    @Resource
    private IAuthApplicationService applicationService;

    @Override
    public List<AuthPermission> getUserPermissionsByApplicationClientId(Long userId, String clientId) {
        final List<AuthApplication> authApplications = authApplicationMapper.selectList(new LambdaQueryWrapper<AuthApplication>().eq(AuthApplication::getClientId, clientId));
        if (CollectionUtils.isNotEmpty(authApplications)) {
            List<AuthRole> authRoles = authRoleMapper.queryByApplicationIdAndUserId(userId, authApplications.get(0).getId());
            if (CollectionUtils.isNotEmpty(authRoles)) {
                return this.baseMapper.queryByApplicationIdAndRoleIds(authApplications.get(0).getId(), authRoles.stream().map(AuthRole::getId).collect(Collectors.toList()));
            }
        }
        return Collections.emptyList();
    }

    @Override
    public String getDataPermissionCode(Long id) {
        if (id == null) {
            return PermissionCodeConstant.authCommon;
        }
        AuthPermission permission = this.getById(id);
        if (!permission.getApplicationId().equals(this.applicationService.getThisApplicationId())) {
            throw new RuntimeException("只能使用授权系统权限");
        }
        return permission.getCode();
    }

    @Override
    public void updateDataPermissionCode(AuthApplication application) {
        final AuthPermission authPermission = new AuthPermission();
        authPermission.setDataPermissionCode(application.getDataPermissionCode());
        authPermission.setLastUpdDt(LocalDateTime.now());
        this.update(authPermission,new LambdaQueryWrapper<AuthPermission>().eq(AuthPermission::getApplicationId,application.getId()));
    }
}
